home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 September / CHIP Eylül 1998.iso / freeware / CDEVE00.EXE / Cdeve00 / CDEvents.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-02-27  |  9.0 KB  |  209 lines

  1. {***************************************************************************}
  2. { [TCDEVENTS]                                                               }
  3. {   Special Units = DBTMsg                                                  }
  4. {   Version = 1.1                                                           }
  5. { [SOURCE]                                                                  }
  6. {   Copyright ⌐ 1998 by Tom Deprez                                          }
  7. {   (Just to prevent to get multiple components, which all have great       }
  8. {    things but would be a GREAT component with a lot of GREAT thing        }
  9. {    That way everybody can benefit of such a GREAT component)              }
  10. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  11. { [COMPONENT]                                                               }
  12. { [properties]                                                              }
  13. { [events]                                                                  }
  14. {   AfterArrival : Fires when new CD is inserted by user                    }
  15. {   AfterRemove  : Fires when CD is removed by user                         }
  16. {                                                                           }
  17. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  18. { [AUTHOR]                                                                  }
  19. {   Author name   = ZifNab (Tom Deprez)                                     }
  20. {   Author e-mail = tom.deprez@uz.kuleuven.ac.be                            }
  21. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  22. { [HISTORY]                                                                 }
  23. {   18/01/1998 : first release                                              }
  24. {   04/02/1998 : changed the order of checking the message (WMDeviceChange) }
  25. {                                                                           }
  26. {***************************************************************************}
  27.  
  28. {***************************************************************************}
  29. {  [COMMENTS]                                                               }
  30. {   I very often visit the Delphi page of the www.Experts-Exchange.com site }
  31. {   It's a place where you can ask or answer questions. One day somebody    }
  32. {   needed to know when a CD is inserted in or ejected out of the CD-Rom    }
  33. {   drive. I found it an interesting question, especially because I could   }
  34. {   not find some 'delphi' source for this problem.                         }
  35. {   I started to search for a solution.                                     }
  36. {   Well, that was the beginning of this component. Where it ends?          }
  37. {   That I don't know, it really depends on the reaction of you! If I get   }
  38. {   some reaction, I'll futher improve it, otherwise it sticks with this.   }
  39. {   Too bad, because I think it can become a great tool.                    }
  40. {                                                                           }
  41. {  [IMPORTANT]                                                              }
  42. {    Made some improvements? : please let me know of, so I can update it    }
  43. {    Need some improvements? : please, just ask and I'll try to make it     }
  44. {    Have you got some ideas? : please, send them to me                     }
  45. {    You use this component? : please, send me an e-mail, why you use this  }
  46. {                              component. An E-mail isn't asked too much.   }
  47. {                              Isn't it? At least let me know of the fact   }
  48. {                              you're using it. It makes me very HAPPY!     }
  49. {  [THANKS]                                                                 }
  50. {    Matvey who brought the first idea to this component                    }
  51. {    Perhaps YOU?                                                           }
  52. {                                                                           }
  53. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  54. {  [COPYRIGHT]                                                              }
  55. {    This file is distributed as freeware and without warranties of any     }
  56. {    kind. You can use it in your own applications at your own risk.        }
  57. {                                                                           }
  58. {  [NOTE]                                                                   }
  59. {    It's freeware, but don't hesitate to send me some money if you've      }
  60. {    become rich with the help of this component ;-)                        }
  61. {    It's freeware, but don't hesitate to send me the program or the         }
  62. {    component, where you use this component in. I would be ever gratefull  }
  63. {    to you, thanks.                                                        }
  64. {***************************************************************************}
  65.  
  66. unit CDEvents;
  67.  
  68. interface
  69.  
  70. uses
  71.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  72.   DBTMsg, DsgnIntf;
  73.  
  74. const cVersion = '1.1';
  75.  
  76. type
  77.  TDeviceChangeEvent = procedure (Sender : TObject; FirstDriveLetter : char)
  78.                          of object;
  79.  
  80.  TAboutZifNabProperty = class(TPropertyEditor)
  81.   public
  82.     procedure Edit; override;
  83.     function GetAttributes: TPropertyAttributes; override;
  84.     function GetValue: string; override;
  85.   end;
  86.  
  87.  TCDEvents = class(TComponent)
  88.   private
  89.     { Private declarations }
  90.      FAbout : TAboutZifNabProperty;
  91.      FWindowHandle: HWND;
  92.      fAfterArrival: TDeviceChangeEvent;
  93.      fAfterRemove: TDeviceChangeEvent;
  94.    procedure WndProc(var Msg: TMessage);
  95.   protected
  96.     { Protected declarations }
  97.      function GetFirstDriveLetter(unitmask:longint):char;
  98.      procedure WMDeviceChange(var Msg : TWMDeviceChange); dynamic;
  99.   public
  100.     { Public declarations }
  101.      constructor Create(AOwner: TComponent); override;
  102.      destructor Destroy; override;
  103.   published
  104.     { Published declarations }
  105.     property About : TAboutZifNabProperty read FAbout write FAbout;
  106.     property AfterArrival : TDeviceChangeEvent read fAfterArrival write fAfterArrival;
  107.     property AfterRemove: TDeviceChangeEvent read fAfterRemove write fAfterRemove;
  108.   end;
  109.  
  110. procedure Register;
  111.  
  112. implementation
  113.  
  114. {*********************** tAboutZifNabproperty component ***********************}
  115.  
  116. procedure TAboutZifNabProperty.Edit;
  117. begin
  118.   Application.MessageBox ('        tCDEvents v'+cVersion
  119.                          +#13#10'This component is freeware.'
  120.                          +#13#10'     ⌐ 1998 ZifNab         '
  121.                          +#13#10''
  122.                          +#13#10'mailto:Tom.Deprez@uz.kuleuven.ac.be',
  123.                          'About',
  124.                          MB_OK+ MB_ICONINFORMATION);
  125. end;
  126.  
  127. function TAboutZifNabProperty.GetAttributes: TPropertyAttributes;
  128. begin
  129.   Result := [paMultiSelect, paDialog, paReadOnly];
  130. end;
  131.  
  132. function TAboutZifNabProperty.GetValue: string;
  133. begin
  134.   Result := '(about)';
  135. end;
  136.  
  137. {**************************** tCDEvents component *****************************}
  138.  
  139. constructor TCDEvents.Create(AOwner: TComponent);
  140. begin
  141.   inherited Create(AOwner);
  142.   FWindowHandle := AllocateHWnd(WndProc);
  143. end;
  144.  
  145. destructor TCDEvents.Destroy;
  146. begin
  147.   DeallocateHWnd(FWindowHandle);
  148.   inherited Destroy;
  149. end;
  150.  
  151. procedure TCDEvents.WndProc(var Msg: TMessage);
  152. begin
  153.      if (Msg.Msg = WM_DEVICECHANGE) then
  154.       try
  155.         WMDeviceChange(TWMDeviceChange(Msg));
  156.       except
  157.         Application.HandleException(Self);
  158.       end
  159.     else
  160.       Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  161. end;
  162.  
  163. function TCDEvents.GetFirstDriveLetter(unitmask : longint):char;
  164. var DriveLetter : shortint;
  165. begin
  166.  DriveLetter := Ord('A');
  167.  while (unitmask and 1)=0  do begin
  168.   unitmask := unitmask shr 1;
  169.   inc(DriveLetter);
  170.  end;
  171.  Result := Char(DriveLetter);
  172. end;
  173.  
  174. procedure TCDEvents.WMDeviceChange(var Msg : TWMDeviceChange);
  175. var lpdb : PDEV_BROADCAST_HDR;
  176.     lpdbv : PDEV_BROADCAST_VOLUME;
  177. begin
  178.  (* received a wm_devicechange message *)
  179.   lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
  180.   (* look at the event send together with the wm_devicechange message *)
  181.    case Msg.Event of
  182.     DBT_DEVICEARRIVAL : begin
  183.       if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
  184.        lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
  185.        if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
  186.         if Assigned(fAfterArrival) then
  187.          fAfterArrival(Self, GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
  188.       end;
  189.      end;
  190.     DBT_DEVICEREMOVECOMPLETE : begin
  191.       if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
  192.        lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
  193.        if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
  194.         if Assigned(fAfterArrival) then
  195.          fAfterRemove(Self, GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
  196.       end;
  197.      end;
  198.    end;
  199.  end;
  200.  
  201. procedure Register;
  202. begin
  203.   RegisterComponents('Lib_Erw', [TCDEvents]);
  204.   RegisterPropertyEditor(TypeInfo(TAboutZifNabProperty), TCDEvents,
  205.                          'ABOUT', TAboutZifNabProperty);
  206. end;
  207.  
  208. end.
  209.